package com.itqf.controller;

import com.itqf.dao.BookDao;
import com.itqf.entity.Book;
import freemarker.template.Template;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.annotation.Resource;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/12/17
 * @Time: 上午11:44
 */
@Controller
public class MyController {

    @Resource
    private BookDao bookDao;


    @Resource
    private FreeMarkerConfigurer freeMarkerConfigurer;


    @RequestMapping("/first")
    public   String   show(Model model){
        model.addAttribute("name","张三");
        model.addAttribute("bookList",bookDao.findAll());
        return "hello"; //templates/hello.ftl
    }


    @RequestMapping("/findBook")
    @ResponseBody
    public List<Book> show(){


        return  bookDao.findAll();
    }

    @RequestMapping("/find")
    @ResponseBody
    public Book findOne(){
            Book book = new Book();
            book.setTitle("测试 我是第三方服务");

             return book;
    }

    @RequestMapping("/second")
    @ResponseBody
    public Map getHtml(Map model){
        Map map = null;
        try {
            //1,得到模板文件
            Template template =  freeMarkerConfigurer.getConfiguration().getTemplate("hello.ftl");

            //2.数据模型
            model.put("name","张三");
            model.put("bookList",bookDao.findAll());

            //3,ftl+data--->html()
            template.process(model,new FileWriter("/Users/liliting/Desktop/四阶段/05-springboot/springboot-day02/list.html"));

            map = new HashMap();
            map.put("msg","静态化成功");
            return  map;
        } catch (Exception e) {
            e.printStackTrace();
        }

        map.put("msg","静态化失败");
        return map;
    }




}
